<?php

if (!file_exists("../vendor/autoload.php")) {
    echo "请在此sdk根目录执行: composer dump-autoload";
    exit;
}

require_once "../vendor/autoload.php";

// 商户私钥
$privateKey = "MIICXQIBAAKBgQDUzrqGLdzRV8kUpQzfPBk/mawhDQ17/Lg3k0hlAj97HpUyFpywjvYTip3lkFn4pGTApgJCz6UGJ/YwPp35D78vT+salguccoWXia0mxzjaQzdNpxiUHjCJs4Z0MbhEXNYkpwgpNHkR1A372K1vXb7BinfwN02gILhyNDNOqAXTTwIDAQABAoGAFEnBLpmgu7SKzxUNLRslrRKRfnUoG/Hz/hhkhny4YJyBzMyrE4oNm79TXldsvG+vy04IWSZCrVuR9PeDSANgTo3E7tJLvd3L8on0Vu/vAoFmGlR9U7jVMW65jUAvh5F0VdDWALwSmAm8D20te/G+vXRyHbuBsEG3Abj+h6MI4wECQQD8G0W/A7NlxmHL6j+DfX2g7VJHG5q2d69lmR43ZclUfeg+1SAeYUxPTRnQYiXXJjvrC0byVZKwOC65QiNglp1fAkEA2BgVcffXnTnJqIBTlhcKq8UThlpiXymEl45xcMjSLt0EzKxAZZuzUCPTjyPoNIoh2HoC2iKCpaHidLQs2pagEQJAKNhaUnpNBH5BtgH7zFf2lK4emHq2dD2r4zANnzgUiGXlzNhG3eUxsd9yATsFmngNWSV4lyaaH4bauxUmGi4MzQJBANfOpYweAjzIu3YGQnPAKy4EMU2Z8mgrJHnfDbefNxkKLYLh+6X96wP744ouR/ZAhTCWGUcT8/nKREhcr/bVOqECQQCxu+DWvUqHiEZOK8Lf2apapFwu+PlYnfE0gf182nr09tsp4btaRc8gH9/Grkv6ZTlkUm2+kT8dl7kjd/963Nnh";

// 由甘饴提供的平台公钥
$publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfzXdx+oFXeKQbWB415oFLV095IAO4YOLdxTTelRKnCycPhGjtdleAZwwfX2hupcJmYU8LsUZlXXY6A04xY80l2SRoUqrYE16qL4L9JBnwQe3Rs6Ds+/5AXiNC9StMu9OzJXsnT3GNePrICq2iCnfnaXRHwxhhveCMRIUtXDmWCwIDAQAB";

// App key
$appKey = "192006250b4c09247ec02edce69f6a2d";

// 商户号
$merNo = "821812175724807";

try {
    $requestParameter = new \GanYiInfo\RequestParameter($merNo);

    // 注册手机号,请替换为真实是手机号
    $requestParameter->setMobile("13600000000");

    // 身份认证信息(无身份认证信息则不用设置)
    // $idCard = new \GanYiInfo\Card\IdCard("3508211996010107522", "张三");
    // $requestParameter->setIdCard($idCard);

    // 储蓄卡(此手机号为银行预留手机号，可能与注册手机号不同)--无储蓄卡则不需添加
    // $debitCard0 = new \GanYiInfo\Card\DebitCard("6228xxx", "13600000000", "中国农业银行");
    // $requestParameter->addDebitCard($debitCard0);

    // 更多储蓄卡(此手机号为银行预留手机号，可能与注册手机号不同)
    // $debitCard1 = new \GanYiInfo\Card\DebitCard("6225xxx", "13600000000", "中国农业银行");
    // $requestParameter->addDebitCard($debitCard1);

    // 信用卡(此手机号为银行预留手机号，可能与注册手机号不同)--无信用卡则不需添加
    // $creditCard0 = new \GanYiInfo\Card\CreditCard("6225xxx", "13600000000");
    // $requestParameter->addCreditCard($creditCard0);

    // 更多信用卡(此手机号为银行预留手机号，可能与注册手机号不同)
    // $creditCard1 = new \GanYiInfo\Card\CreditCard("62284xxx", "13600000000");
    // $requestParameter->addCreditCard($creditCard1);

    $toSignArr = $requestParameter->toArray();

    if (isset($toSignArr["idcard"])) {
        $toSignArr["idcard"] = json_encode($toSignArr["idcard"], JSON_UNESCAPED_UNICODE);
    }
    if (isset($toSignArr["creditcards"])) {
        $toSignArr["creditcards"] = json_encode($toSignArr["creditcards"], JSON_UNESCAPED_UNICODE);
    }
    if (isset($toSignArr["debitcards"])) {
        $toSignArr["debitcards"] = json_encode($toSignArr["debitcards"], JSON_UNESCAPED_UNICODE);
    }

    $signature = new \GanYiInfo\Utils\Signature();
    $requestParameter->setSign($signature->generateSign($toSignArr, $privateKey, $appKey));

    // 测试环境接口地址, 正式环境地址请查看接口文档
    $betaBaseUrl = "http://47.92.153.166:8006";
    $request = new \GanYiInfo\Request("POST", "/api/user/login-register", $requestParameter->toArray());
    $request->setBaseUrl($betaBaseUrl);

    $client = new \GanYiInfo\Client();
    $response = $client->sendRequest($request);

    if (!$response->isSuccessful()) {
        echo "Request failed." . $response->getMessage();
        exit;
    }

    if (!$signature->verify($response->getDecodedBody(), $response->getSign(), $publicKey, $appKey)) {
        echo "Sign invalid.";
        exit;
    }

    $redirectUrl = $response->getRedirectUrl();
    header("Location: {$redirectUrl}");
    exit();
} catch (\Exception $e) {
    var_dump($e->getTraceAsString());
}